package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.bean.Classinfo;
import com.ycsj.goldmedalnewconcept.bean.TeacherManageResponse;
import com.ycsj.goldmedalnewconcept.bean.TeacherManageResponse.listdetail;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.view.CircleImageView;
import com.ycsj.goldmedalnewconcept.R;

//教学与反思教师列表
public class TeacherFansiListActivity extends Activity implements
		OnClickListener {

	private GridView gv_filemanage;
	private TeacherListAdapter mAdapter;
	private List<listdetail> list = new ArrayList<listdetail>();
	private String account;
	private String role;
	private ImageView iv_back;
	private Intent intent;
	private String user_id;
  private TextView tv_title;
	private String view_type;
	private String teachershowtype = "0";
	private TextView tv_empty;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_teacherfansilist);

		intent = getIntent();
		teachershowtype = intent.getStringExtra("teachershowtype");
		initView();
		setOnListener();
		initData();

		mAdapter = new TeacherListAdapter(list, getBaseContext());
		gv_filemanage.setAdapter(mAdapter);
	}

	private void initView() {
		gv_filemanage = (GridView) findViewById(R.id.gv_filemanage);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_empty = (TextView) findViewById(R.id.tv_empty);
		tv_title = (TextView) findViewById(R.id.tv_title);

		if("2".equals(teachershowtype)){
			tv_title.setText("家长评价");
		}
	
		
		gv_filemanage.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if ("1".equals(teachershowtype)) {
					intent = new Intent(getBaseContext(),
							TeacherShowActivity.class);
					intent.putExtra("user_id", list.get(position).userid);
					startActivity(intent);
				} else if ("2".equals(teachershowtype)) {
					Intent intent=new Intent(getBaseContext(), ParentsEvaluationActivity.class);
					intent.putExtra("TEACHER_MOBILE",list.get(position).userid);
					startActivity(intent);
				} else if ("3".equals(teachershowtype)) {
					Intent intent=new Intent(getBaseContext(), TeacherManageActivity.class);
					intent.putExtra("user_id",list.get(position).userid);
					startActivity(intent);
				}else {
					intent = new Intent(getBaseContext(), ClassListActivity.class);// 单元选择界面
					intent.putExtra("view_type", "5");
					intent.putExtra("user_id", list.get(position).userid);
					startActivity(intent);
				}
			}
		});
	}

	private void initData() {
		account = SPUtil.getString(this, "account", "");
		role = SPUtil.getString(this, "role", "");
		OkHttpClient mOkHttpClient = new OkHttpClient();
		// mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "15");
		builder.add("c", account);
		builder.add("role", role);
		Request request = new Request.Builder()
				.url(Constant.ycsj_count_MessageCenter)
				.post(builder.build()).build();
		mOkHttpClient.newCall(request).enqueue(new Callback() {

			@Override
			public void onFailure(Request arg0, IOException arg1) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(getBaseContext(), "网络无响应，请检查网络连接", 0)
								.show();
					}
				});
			}

			@Override
			public void onResponse(Response arg0) throws IOException {
				// 解析Json数据，保存登录状态及身份信息

				Gson gson = new Gson();

				String json = arg0.body().string();
				TeacherManageResponse fromJson = gson.fromJson(json,
						TeacherManageResponse.class);
				Log.e("test11", json);
				if (fromJson != null) {
					if ("-1".equals(fromJson.state)
							|| "0".equals(fromJson.state)) {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								tv_empty.setVisibility(tv_empty.VISIBLE);
							}
						});
						return;
					} else {
						list = fromJson.list;

						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								mAdapter = new TeacherListAdapter(list,
										getBaseContext());
								gv_filemanage.setAdapter(mAdapter);
							}
						});
					}
				}
			}
		});
	}

	private void setOnListener() {
		iv_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;
		}
	}

	class TeacherListAdapter extends BaseAdapter {
		private LayoutInflater inflater;
		private List<listdetail> list;

		public TeacherListAdapter(List<listdetail> list, Context context) {
			super();
			this.list = list;
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			if (null != list) {
				return list.size();
			} else {
				return 0;
			}
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.gv_teacherlist_item,
						null);
				viewHolder = new ViewHolder();
				viewHolder.name = (TextView) convertView
						.findViewById(R.id.name);
				viewHolder.icon = (CircleImageView) convertView
						.findViewById(R.id.image);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			if ("0".equals(list.get(position).sex)) {
				Picasso.with(TeacherFansiListActivity.this)
						.load(list.get(position).icon)
						.error(R.drawable.teacher_header0)
						.into(viewHolder.icon);
			}
			if ("1".equals(list.get(position).sex)) {
				Picasso.with(TeacherFansiListActivity.this)
						.load(list.get(position).icon)
						.error(R.drawable.teacher_header1)
						.into(viewHolder.icon);
			}
			viewHolder.name.setText(list.get(position).name);

			return convertView;
		}
	}

	class ViewHolder {
		public TextView name;
		public CircleImageView icon;

	}
}
